const serviceUrlPrefix = "https://teda-heart.asia/mobilebi"; // 生产环境
// const serviceUrlPrefix = "http://172.20.116.106:8063/mobilebi"; // 本地环境
// const serviceUrlPrefix = "http://172.20.10.211:8861/mobilebi"; // 测试环境_211
// const serviceUrlPrefix = "http://172.20.116.173:8063/mobilebi"; // 开发环境_程如鑫



function httpPost(apiUrl, data, successFunction, failFunction) {
	return uni.request({
		url: serviceUrlPrefix + apiUrl,
		data: JSON.stringify(data),
		method: 'POST',
		success: function(res) {
			if (res.data.status == 0) {
				successFunction(res.data)
			} else {
				uni.showToast({
					icon: 'none',
					title: res.data.message,
					duration: 3000
				})

				failFunction(res.data)
			}
		},
		fail: function(e) {
			console.log(serviceUrlPrefix + apiUrl + "请求失败！")
			console.log(e)
		}
	})
}

function httpGet(apiUrl, successFunction) {
	uni.request({
		url: serviceUrlPrefix + apiUrl,
		success: (res) => {
			successFunction(res.data)
		},
		fail: function(e) {
			console.log(serviceUrlPrefix + apiUrl + "请求失败！")
			console.log(e)
		}
	});
}

function dateToString(date, type) {
	type = type == undefined ? "-" : type;
	date = date || new Date();
	let year = new Date(date).getFullYear();
	let month = new Date(date).getMonth() + 1;
	let day = new Date(date).getDate();
	if (month < 10) {
		month = "0" + month.toString();
	}
	if (day < 10) {
		day = "0" + day.toString();
	}
	date = year + type + month + type + day
	return date
}

function getDate(dateString, days, flag) {
	let trueDays = days == undefined ? 1 : days;
	dateString = new Date(dateString);
	if (flag && flag === "-") {
		dateString = +dateString - 1000 * 60 * 60 * 24 * trueDays;
	} else {
		dateString = +dateString + 1000 * 60 * 60 * 24 * trueDays;
	}
	dateString = new Date(dateString);

	let year = dateString.getFullYear();
	let month = dateString.getMonth() + 1;
	let day = dateString.getDate();

	if (month < 10) {
		month = "0" + month.toString();
	}
	if (day < 10) {
		day = "0" + day.toString();
	}

	//格式化
	return year + "-" + month + "-" + day;
}

function getNextDayByString(dateString) {
	return getDate(dateString, 1, "+")
}

function getPreDayByString(dateString) {
	return getDate(dateString, 1, "-")
}

function getLastWeekByString(dateString) {
	return getDate(dateString, 6, "-")
}

function getLastMonthByString(dateString) {
	return getDate(dateString, 29, "-")
}

function getLastYearByString(dateString) {
	return getDate(dateString, 365, "-")
}

// 获取上个月的最后一天
function getLastMonthLastDay() {
	let nowdays = new Date();
	let year = nowdays.getFullYear();
	let month = nowdays.getMonth();
	if(month==0){
		month = 12;
		year = year-1;
	}
	if(month<10){
		month = '0'+month;
	}
	let myDate = new Date(year,month,0);
	return year+'-'+month+'-'+myDate.getDate()//上个月最后一天
}

function columnChangeToPie(columnChartData) {
	let pieChartData = {};
	if(columnChartData && columnChartData["categories"] && columnChartData["series"] && columnChartData["series"][0] && columnChartData["series"][0]["data"]) {
		let serie = columnChartData["series"][0];
		let pieData = [];
		for (var i = 0; i < columnChartData["categories"].length; i++) {
			let name = columnChartData["categories"][i];
			let value = Number(serie["data"][i]);
			pieData.push({name: name, value: value})
		}
		pieChartData["series"] = [];
		pieChartData["series"][0] = {data: pieData}
	}
	return pieChartData
}

// Y轴不显示数字
function diy_notShowYData(chartData) {
	if(chartData["series"]) {
		for (var i = 0; i < chartData["series"].length; i++) {
			let serie = chartData["series"][i];
			serie["textSize"] = 1
		}
	}
}

// 手动取消该请求任务
function diy_abortRequest(requestTask) {
	if(requestTask) {
		requestTask.abort();
		requestTask = undefined
	}
}

export default {
	httpPost,
	httpGet,
	dateToString,
	getPreDayByString,
	getNextDayByString,
	getLastWeekByString,
	getLastMonthByString,
	getLastYearByString,
	getLastMonthLastDay,
	columnChangeToPie,
	diy_notShowYData,
	diy_abortRequest
}
